Lab5


4531201521_4531202121  นาย กฤษฎา เฉลิมสุข และ นาย เกรียงยุทธ หวังจิตมั่น (13/8/2545 (11:36:47))
(SM=3, CM=35, ST=41, KY=0, TR=05:39)

TestScript
Mini-Quiz :  (0.0 คะแนน)

JLab>javac Lab5.java
JLab>
JLab>java Selftest
>>JLabIO->Testing (2011, 7) : 2.00
>>JLabIO->Testing (2008, 9) : 2.00
>>JLabIO->Testing (2005, 12) : 2.00
>>JLabIO->Testing (1998, 1) : 2.00
>>JLabIO->Testing (2011, 5) : 2.00

>>JLab:<POINT>10.00</POINT>
JLab>

ได้ 10 คะแนน
Source Code
/**
 * 2110101 Computer Programming
 * Lab #5 : Loops : while, do-while, for
 */
import java.util.*;
import jlab.JLabIO;

public class Lab5 {
  public static void calendar(int year, int month) {
    // Example
    //-----------------------------
    //        August(2002)
    //-----------------------------
    // SUN MON TUE WED THU FRI SAT
    //                   1   2   3     \
    //   4   5   6   7   8   9  10      |
    //  11  12  13  14  15  16  17       >  your code
    //  18  19  20  21  22  23  24      |
    //  25  26  27  28  29  30  31     /
    //=============================
    
    // print header
    System.out.println(LONGLINE);
    System.out.println("        " + monthName[month - 1] + "(" + year + ")");
    System.out.println(LONGLINE);
    System.out.println(" SUN MON TUE WED THU FRI SAT");
    
    Calendar calendar = new GregorianCalendar(new Locale("th", "TH"));
    calendar.set((year > 2500 ? year - 543 : year), month - 1, 1);
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    int numDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    
    // add your code here to format and print the month
    // Use the following two variables :
    //   - numDays is the number of days in this month
    //   - firstDay is the day-of-week of the first of this month
    
    for (int i = 1; i < firstDay; i++) {
      System.out.print("    ");
      }
    String a;
    for (int i = 1; i <= numDays; i++) {
      a = JLabIO.format (i, 4);
      System.out.print(a);
      if (i % 7 == (8 - firstDay) % 7) {
        System.out.println();
      }
      if (i == numDays) System.out.println();
    }
    // add a line to notify the end of printing
    System.out.println(LASTLINE);
  }
  
  //----------------------------------------------------------------
  public static void main(String[] args) {
    int y = JLabIO.readInt("Year = ");
    int m = JLabIO.readInt("Month = ");

    if (y > 0 && (1 <= m && m <= 12)) {
      calendar(y, m);
    } else {
      System.out.println("error : invalid year or month");
    }
  }
  //----------------------------------------------------------------
  
  final static String[] monthName =
     {"January", "February", "March", "April",
      "May", "June", "July", "August",
      "September", "October", "November", "December"};
  final static String LONGLINE = "-----------------------------";
  final static String LASTLINE = "=============================";
  
}